<?php
	Class Utils{
		
		/**
		 * 
		 * Static util function used to get day of week base on a number
		 * 
		 * @param String $num
		 */
		public static function getDayofWeek($num){
			$dayOfWeek = '';
			switch ($num){
				case '0':
					$dayOfWeek = 'Chủ Nhật';
					$break;
				case '1';
					$dayOfWeek = "Thứ Hai";
					break;
				case '2':
					$dayOfWeek = "Thứ Ba";
					break;
				case '3':
					$dayOfWeek = "Thứ Tư";
					break;
				Case '4':
					$dayOfWeek = "Thứ Năm";
					break;
				case '5':
					$dayOfWeek = "Thứ Sáu";
					break;
				case '6':
					$dayOfWeek = "Thứ Bảy";
					break;
					
			}
			return $dayOfWeek;
		}
		
		/**
		 * 
		 * Static util function used to get pagination information
		 * 
		 * @param int $pageNo
		 * @param int $totalRec
		 * @param int $startPage
		 * @param int $endPage
		 */
		public static function getPagerInfo($pageNo, $totalRec, $startPage, $endPage){
			$paginator = array();
			$paginator['page_no'] = $pageNo;
			
			$totalPage = floor($totalRec/5);
			if ($totalRec % 5 > 0){
				$totalPage = $totalPage + 1;
			}
			$paginator['total_page'] = $totalPage;
			
			if ($endPage > $totalPage){
				$endPage = $totalPage;
			}
			
			$paginator['start_index'] = $pageNo * 5 - 5;
			
			if($pageNo = $endPage){
				$outStartPage = $startPage + 1;
				if ($outStartPage > $pageNo){
					$outStartPage = 1;
				}
				$paginator['start_page'] = $outStartPage;
				$outEndPage = $endPage + 1;
				if ($outEndPage > $totalPage) {	
					$paginator['end_page'] = $endPage;
				}else{
					$paginator['end_page'] = $outEndPage;
				}
			}else{
				$paginator['start_page'] = $startPage;
				$paginator['end_page'] = $endPage;
			}
			
			return $paginator;
		}
		
		/**
		 * Util function used to check session
		 * 
		 * @param Controller $controller
		 */
		public static function checkSession(Controller $controller){
			if(session_id() == '') {
				session_start();
			}
			if(!isset($_SESSION['user_id'])){
				$controller->redirect(array('controller'=>'Users',
					'action'=>'login'));
			}
		}
		
		/**
		 * Util function used to encode string
		 * 
		 * @param String $value
		 */
		public static function encodeString($value){
			$value = str_replace('\"', '"', $value);
			return htmlentities($value, ENT_QUOTES, 'utf-8');
		}
		
		/**
		 * Util function used to encode string
		 * 
		 * @param String $value
		 */
		public static function decodeString($value){
			return html_entity_decode($value, ENT_QUOTES, 'utf-8');
		}
		
		/**
		 * Util function used to encode string
		 * 
		 * @param String $value
		 */
		public static function encodeHtml($value){
			return htmlentities($value, ENT_HTML5, 'utf-8');
		}
		
		/**
		 * Util function used to encode string
		 * 
		 * @param String $value
		 */
		public static function encodeSql($value){
			return str_replace("'", "\'", $value);
		}
		
	}
?>